#! /bin/sh
set -e

# Files
LG="/etc/locale.gen"
EE="/etc/default/locale"

# Sanitize environnement
LC_ALL=C
LANG=C

# Load debconf
. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup multiselect

# Conversion of locales that have been removed 
convert_locale()
{
    echo "$1" | sed -e "s/no_NO/nb_NO/g" -e 's/ks_IN/ks_IN@devanagari/g' -e 's/iw_IL/he_IL/g'
}

# List of locales provided by the current version
PROVIDED_LOCALES="__PROVIDED_LOCALES__"

# List of locales provided by the user
if [ -f /usr/local/share/i18n/SUPPORTED ] ; then
    USER_LOCALES="$(sed -e '/^[a-zA-Z]/!d' -e 's/ *$//g' /usr/local/share/i18n/SUPPORTED)"
fi

# List of locales in /etc/locale.gen
if [ -e $LG ]; then
  GEN_LOCALES="$(sed -e '/^[a-zA-Z]/!d' -e 's/ *$//g' $LG)"
  GEN_LOCALES="$(convert_locale "$GEN_LOCALES")"
fi

# List of supported locales (PROVIDED_LOCALES + USER_LOCALES + GEN_LOCALES)
SUPPORTED_LOCALES="$(printf '%s\n' "$PROVIDED_LOCALES" "$USER_LOCALES" "$GEN_LOCALES" | grep -v "^$" | sort -u | tr '\n' ',' | sed -e 's/, */, /g' -e 's/, *$//g')"
db_subst locales/locales_to_be_generated locales "$SUPPORTED_LOCALES"

# Get the list of selected locales from /etc/locale.gen
if [ -e /etc/locale.gen ]; then
    if [ -L $LG ] && [ "$(readlink $LG)" = "/usr/share/i18n/SUPPORTED" ]; then
        SELECTED_LOCALES="All locales"
    else
        SELECTED_LOCALES="$(echo "$GEN_LOCALES" | sort -u | tr '\n' ',' | sed -e 's/, */, /g' -e 's/, *$//g')"
    fi
    db_set locales/locales_to_be_generated "$SELECTED_LOCALES"
fi

DEFAULT_ENVIRONMENT="$(cat /etc/environment /etc/default/locale 2>/dev/null | awk '/^LANG=/ {gsub("\"", ""); sub("LANG=", ""); lang=$0;} END {print lang}')"
DEFAULT_ENVIRONMENT="$(convert_locale "$DEFAULT_ENVIRONMENT")"
if [ -n "$SUPPORTED_LOCALES" ] && [ -n "$DEFAULT_ENVIRONMENT" ]; then
  if echo "$SUPPORTED_LOCALES" | grep -q -e "\b$DEFAULT_ENVIRONMENT\b" ; then
    db_set locales/default_environment_locale "$DEFAULT_ENVIRONMENT"
  fi
fi

STATE=1
while [ "$STATE" -ge 0 ]; do
    case "$STATE" in
    0)
        exit 1
        ;;
    1)
        db_input medium locales/locales_to_be_generated || true
        ;;
    2)
        db_get locales/locales_to_be_generated || RET=
        if expr ", $RET," : ".*, None,.*" >/dev/null 2>&1; then
            # "None" was a choice in older packages
            db_set locales/locales_to_be_generated ""
            RET=
        elif expr ", $RET," : ".*, All locales,.*" >/dev/null 2>&1; then
            # When "All locales" is selected, other choices have to be dropped
            db_set locales/locales_to_be_generated "All locales"
            RET=$SUPPORTED_LOCALES
        fi
        DEFAULT_LOCALES="$(echo $RET | sed -e 's/ [^ ]*,/,/g' -e 's/ [^ ]*$//')"
        if [ -n "$DEFAULT_LOCALES" ]; then
            db_subst locales/default_environment_locale locales $DEFAULT_LOCALES
            db_input medium locales/default_environment_locale || true
        fi
        ;;
    *)
        break
        ;;
    esac
    if db_go; then
        STATE=$(($STATE + 1))
    else
        STATE=$(($STATE - 1))
    fi
done
